
//---------------------------------------------------------------------------
#include "test_game.h"
//---------------------------------------------------------------------------
#include "rendering\pg1_renderer.h"
#include "input\pg1_input.h"
#include "animation\pg1_timer.h"
#include "importer\pg1_importer.h"
//---------------------------------------------------------------------------
using namespace test;
//---------------------------------------------------------------------------
bool Game::init (pg1::Renderer& rkRenderer)
{
	// obstacles
	m_pkObstacle1.setScale(50.0f, 300.0f);
	m_pkObstacle1.setPos(200.0f, 0.0f);

	m_pkObstacle2.setScale(600.0f, 100.0f);
	m_pkObstacle2.setPos(0.0f, -200.0f);

	m_apkObstacles.push_back(&m_pkObstacle1);
	m_apkObstacles.push_back(&m_pkObstacle2);

	// mario
	m_kIdle.setLength(1000.0f);
	m_kIdle.addFrame(512.0f, 512.0f, 5.0f, 112.0f, 18.0f, 32.0f);

	m_kWalk.setLength(400.0f);
	m_kWalk.addFrame(512.0f, 512.0f, 24.0f, 112.0f, 18.0f, 32.0f);
	m_kWalk.addFrame(512.0f, 512.0f, 42.0f, 112.0f, 18.0f, 32.0f);

	m_kMario.setScale(90.0f, 160.0f);
	m_kMario.setPos(0.0f, 100.0f);
	m_kMario.setAnimation(&m_kIdle);

	pg1::Texture kCharactersTexture = rkRenderer.loadTexture( "assets/characters.png", PG1_COLOR_RGB(0,255,0) );
	m_kMario.setTexture(kCharactersTexture);

	pg1::Importer::importMap(m_kMap, rkRenderer, "assets/mapa.tmx");

	return true;
}
//---------------------------------------------------------------------------
void Game::frame (pg1::Renderer& rkRenderer, pg1::Input& rkInput, pg1::Timer& rkTimer)
{
	static float fMarioInitialScaleX = m_kMario.scaleX();

	// handle user input
	static float fSpeed = 100.0f * rkTimer.timeBetweenFrames();

	if( rkInput.keyDown(pg1::Input::KEY_RIGHT) )
	{
		m_kMario.setPos( m_kMario.posX() + fSpeed, m_kMario.posY() );
		m_kMario.setAnimation(&m_kWalk);

		m_kMario.setScale( fMarioInitialScaleX, m_kMario.scaleY() );
	}
	else if( rkInput.keyDown(pg1::Input::KEY_LEFT) )
	{
		m_kMario.setPos( m_kMario.posX() - fSpeed, m_kMario.posY() );
		m_kMario.setAnimation(&m_kWalk);

		m_kMario.setScale( -fMarioInitialScaleX, m_kMario.scaleY() );
	}
	else if( rkInput.keyDown(pg1::Input::KEY_DOWN) )
	{
		m_kMario.setPos( m_kMario.posX(), m_kMario.posY() - fSpeed );
		m_kMario.setAnimation(&m_kWalk);
	}
	else if( rkInput.keyDown(pg1::Input::KEY_UP) )
	{
		m_kMario.setPos( m_kMario.posX(), m_kMario.posY() + fSpeed );
		m_kMario.setAnimation(&m_kWalk);
	}
	else
	{
		m_kMario.setAnimation(&m_kIdle);
	}

	// check collisions
	for(unsigned int i=0; i<m_apkObstacles.size(); i++)
	{
		pg1::Entity2D& rkObstacle = *( m_apkObstacles[i] );

		if( m_kMario.checkCollision(rkObstacle) == pg1::Entity2D::CollisionHorizontal )
		{
			m_kMario.returnToPos( m_kMario.previousPosX(), m_kMario.posY() );
		}
		else if( m_kMario.checkCollision(rkObstacle) == pg1::Entity2D::CollisionVertical )
		{
			m_kMario.returnToPos( m_kMario.posX(), m_kMario.previousPosY() );
		}
	}

	// update animation
	m_kMario.update(rkTimer);

	// render
	m_kMap.draw(rkRenderer);
	m_pkObstacle1.draw(rkRenderer);
	m_pkObstacle2.draw(rkRenderer);
	m_kMario.draw(rkRenderer);
	
	m_pkObstacle1.drawAABB(rkRenderer);
	m_pkObstacle2.drawAABB(rkRenderer);
	m_kMario.drawAABB(rkRenderer);
}
//---------------------------------------------------------------------------
void Game::deinit ()
{
	// nothing to do
}
//---------------------------------------------------------------------------
